﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  Context.cs
//    
// Abstract:
//
//  This module implements the Context and ExtendedContext classes.
//  
//--

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.OfficeCommunicationsServer.Applications.Common
{
    /// <summary>
    /// Context for a log message. Each context has a unique ID that will be included in the log message and used to correlate
    /// logs related to a particular activity.
    /// </summary>
    [Serializable]
    public class Context
    {
        // Global context to be used for isolated log messages not associated with a particular activity.
        private static Context _globalContext = new Context(Guid.Empty);

        // Unique ID associated with this context.
        private Guid _activityId;

        /// <summary>
        /// ctor.
        /// </summary>
        /// <param name="activityId">Activity ID</param>
        protected Context(Guid activityId)
        {
            _activityId = activityId;
        }

        /// <summary>
        /// ctor.
        /// </summary>
        public Context() : this(Guid.NewGuid())
        {
        }

        /// <summary>
        /// Returns the global context.
        /// </summary>
        public static Context Global
        {
            get
            {
                return _globalContext;
            }
        }

        /// <summary>
        /// Returns the activity ID associated with this context.
        /// </summary>
        public Guid ActivityId
        {
            get
            {
                return _activityId;
            }
            protected set
            {
                _activityId = value;
            }
        }

        /// <summary>
        /// When overridden in a derived class this should return the values of the additional properties encapsulated by 
        /// the derived class.
        /// </summary>
        /// <returns>null</returns>
        public virtual string ExtendedPropertiesToXmlString()
        {
            return null;
        }

        /// <summary>
        /// Renders the contents of this object to a string.
        /// </summary>
        /// <remarks>
        /// This method is used to write the context to the System.Diagnostics.Trace logs.
        /// </remarks>
        /// <returns>
        /// A string representation of this object.
        /// </returns>
        public override string ToString()
        {
            return _activityId.ToString();
        }
    }
}
